using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Microsoft.Win32;
using ArchSoft;
using System.Windows.Forms;

namespace ArchSoft
{
    public class InsertViewport
    {
         string _sourcePath;
        List<string> EditedFiles_;

        public InsertViewport(string source, List<string> EditedFiles)
        {
            _sourcePath = source;
            EditedFiles_ = EditedFiles;
        }

        public bool DeleteTarget()
        {
            // Delete .bak file
            try
            {
                DirectoryInfo source = new DirectoryInfo(_sourcePath);
                foreach (DirectoryInfo subDir in source.GetDirectories())
                {
                    foreach (FileInfo file in subDir.GetFiles())
                    {
                        if (!file.Extension.Equals(".dwg") && !file.Extension.Equals(".dwt"))
                        {
                            file.Delete();
                        }
                    }
                }
                return true;
            }
            catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return false; }
        }

        private void DeleteTempFile()
        {
            DirectoryInfo issueWindow = new DirectoryInfo(_sourcePath);
            foreach (FileInfo adrawing in issueWindow.GetFiles())
                adrawing.Delete();

            foreach (DirectoryInfo dir in issueWindow.GetDirectories())
                if (dir.Name.ToLower().Contains("reference"))
                    dir.Delete(true);
                else
                {
                    foreach (FileInfo adrawing in dir.GetFiles())
                        if (!adrawing.Extension.Contains("dwg") && !adrawing.Extension.Contains("dwt"))
                            adrawing.Delete();
                }
        }

        public string GenerateScriptFile()
        {
            string tempFileName = _sourcePath + @"\" + Guid.NewGuid().ToString() + ".scr";

            FileStream scriptFile = File.Open(tempFileName, FileMode.CreateNew);
            StreamWriter wr = new StreamWriter(scriptFile);

            wr.WriteLine("filedia 0");


            for (int i = 0; i < EditedFiles_.Count - 1; i++)
            {
                ScriptForDetachTitle(wr, false, EditedFiles_[i].Split('*')[0], EditedFiles_[i].Split('*')[1]);
            }

            ScriptForDetachTitle(wr, true, EditedFiles_[EditedFiles_.Count - 1].Split('*')[0], EditedFiles_[EditedFiles_.Count - 1].Split('*')[1]);

            wr.WriteLine("filedia 1");
            wr.WriteLine("quit");
            wr.Flush();
            wr.Dispose();

            scriptFile.Close();
            scriptFile.Dispose();
            return tempFileName;
        }

        public string RunSciptFile(string filePath)
        {
            AutocadHelper helper = AutocadHelper.Instance;
            return helper.RunAutoCadScript(filePath, true);
        }

        public string Process()
        {
            string str = "";

            bool tpd = DeleteTarget(); if (!tpd) { return ""; }
            string scriptFile = GenerateScriptFile();
            if (scriptFile == "")
                return "";
            str = RunSciptFile(scriptFile);
            DeleteTempFile();
            return str;
        }

        public void ScriptForDetachTitle(StreamWriter wr, bool lastDrawing, string fileName, string ScaleI)
        {
            string sfi = _sourcePath + "\\Xref\\A_Viewport.dwg";
            if (!File.Exists(sfi))
                sfi = "Null";
            //Insert Viewport;
            wr.WriteLine("open \"" + fileName + "\"");
            wr.WriteLine("TILEMODE");
            wr.WriteLine("1");
            wr.WriteLine("-INSERT");

            wr.WriteLine("\"" + sfi + "\"");
            wr.WriteLine("0,0");
            wr.WriteLine(ScaleI);//Drawing Scale
            wr.WriteLine(ScaleI);//Drawing Scale
            wr.WriteLine("0");
            wr.WriteLine("QSAVE");

            if (!lastDrawing)
            {
                wr.WriteLine("Close");
            }
        }

        public void ScriptForInsertViewport(StreamWriter wr, bool lastDrawing, string fileName)
        {           
           
        }
    }
}

////Synchronize Dimension;
//wr.WriteLine("open \"" + fileName + "\"");
//wr.WriteLine("-DIMSTYLE");
//wr.WriteLine("Save");
//wr.WriteLine("10");//Follow Drawing Scale
//wr.WriteLine("DIMSCALE");
//wr.WriteLine("10");//Follow Drawing Scale
////Synchronize Dimension Redefine;
//wr.WriteLine("-DIMSTYLE");
//wr.WriteLine("Save");
//wr.WriteLine("10");//Follow Drawing Scale
//wr.WriteLine("Y");
//wr.WriteLine("DIMSCALE");
//wr.WriteLine("10");//Follow Drawing Scale
//wr.WriteLine("QSAVE"); -------------------------------

////Synchronize Lineweight;
//wr.WriteLine("open \"" + fileName + "\"");
//wr.WriteLine("-LAYER");
//wr.WriteLine("LWEIGHT");
//wr.WriteLine("DEFAULT");
//wr.WriteLine("*");
//wr.WriteLine("");
//wr.WriteLine("QSAVE"); --------------------------------

////Synchronize Layer;
//wr.WriteLine("open \"" + fileName + "\"");
//wr.WriteLine("-LAYER");
//wr.WriteLine("N");//Name
//wr.WriteLine("A-View");
////wr.WriteLine("");
//wr.WriteLine("C");//Color
//wr.WriteLine("2");
//wr.WriteLine("A-View");
////wr.WriteLine("");
//wr.WriteLine("Ltype");//Linetype
//wr.WriteLine("2");
//wr.WriteLine("A-View");
//wr.WriteLine("");
//wr.WriteLine("QSAVE");---------------------------

////Synchronize Linetype;
//wr.WriteLine("open \"" + fileName + "\"");
//wr.WriteLine("-LINETYPE");
//wr.WriteLine("LOAD");
//wr.WriteLine("*");
//wr.WriteLine("\"" + "C:\Documents and Settings\Trang 02\Application Data\Trang\Architecture\Trang Linetype.lin" + "\"");
//wr.WriteLine("");
//wr.WriteLine("QSAVE");------------------------------

////Synchronize Scale;
//wr.WriteLine("open \"" + fileName + "\"");
//wr.WriteLine("LTScale");
//wr.WriteLine("20");//Follow Drawing Scale
//wr.WriteLine("QSAVE");---------------------------------

////Hide Wipeout;
//wr.WriteLine("open \"" + fileName + "\"");
//wr.WriteLine("WIPEOUT");
//wr.WriteLine("F");
//wr.WriteLine("OFF");
//wr.WriteLine("QSAVE");---------------------------------

////Reload All Drawing;
//wr.WriteLine("open \"" + fileName + "\"");
//wr.WriteLine("-XREF");
//wr.WriteLine("RELOAD");
//wr.WriteLine("*");
//wr.WriteLine("QSAVE");---------------------------------

////Unload All Drawing;
//wr.WriteLine("open \"" + fileName + "\"");
//wr.WriteLine("-XREF");
//wr.WriteLine("UNLOAD");
//wr.WriteLine("*");
//wr.WriteLine("QSAVE");

////Change Text Case;
//wr.WriteLine("open \"" + fileName + "\"");
//wr.WriteLine("-TCASE");
//wr.WriteLine("ALL");
//wr.WriteLine("");
//wr.WriteLine("Title");//Follow the options S:Sentence U:Upper L:Lower T:Title G:Toggle
//wr.WriteLine("QSAVE");

////Set System Variables;
//wr.WriteLine("open \"" + fileName + "\"");
//wr.WriteLine("FILEDIA");
//wr.WriteLine("1");
//wr.WriteLine("BINDTYPE");
//wr.WriteLine("0");
//wr.WriteLine("DIMASSOC");
//wr.WriteLine("1");
//wr.WriteLine("HIGHLIGHT");
//wr.WriteLine("1");
//wr.WriteLine("MIRRTEXT");
//wr.WriteLine("0");
//wr.WriteLine("PSLTSCALE");
//wr.WriteLine("0");
//wr.WriteLine("UCSFOLLOW");
//wr.WriteLine("0");
//wr.WriteLine("VISRETAIN");
//wr.WriteLine("1");
//wr.WriteLine("XREFTYPE");
//wr.WriteLine("0");
//wr.WriteLine("QSAVE");---------------------------------

////Design Viewport;
//wr.WriteLine("open \"" + fileName + "\"");
//wr.WriteLine("TILEMODE");
//wr.WriteLine("1");
//wr.WriteLine("-INSERT");
//wr.WriteLine("\"" + Vfl + "\"");//Attention Path of Viewport Drawing
//wr.WriteLine("0,0");
//wr.WriteLine("");//Drawing Scale
//wr.WriteLine("");//Drawing Scale
//wr.WriteLine("");
//wr.WriteLine("EXPLODE");
//wr.WriteLine("LAST");
//wr.WriteLine("");
//wr.WriteLine("");
//wr.WriteLine("QSAVE");-----------------------------------

//Synchronize Layout;
//Insert Layout
//wr.WriteLine("open \"" + fileName + "\"");
//wr.WriteLine("-LAYOUT");
//wr.WriteLine("T");
//wr.WriteLine("\"" + Vfl + "\"");//Select Template File
//wr.WriteLine("50");//Select Layout Name ------------------------------

////Delete Old Layout
//wr.WriteLine("");
//wr.WriteLine("D");
//wr.WriteLine("Trang");

////Rename Layout
//wr.WriteLine("");
//wr.WriteLine("R");
//wr.WriteLine("");
//wr.WriteLine("A2-01");
//wr.WriteLine("QSAVE");